package com.sys.controller.admin;

import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.core.page.TableDataInfo;
import com.sys.common.enums.BusinessType;
import com.sys.system.domain.SysNotice;
import com.sys.system.domain.vo.SysNoticeVO;
import com.sys.system.service.INoticeUserService;
import com.sys.system.service.ISysNoticeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告 信息操作处理
 *
 * @author LGY
 */
@Tag(name = "通知公告API")
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {

    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private INoticeUserService noticeUserService;

    /**
     * 获取通知公告列表
     */
    @Operation(summary = "获取通知公告列表")
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysNotice notice) {
        startPage(notice);
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    @Operation(summary = "获取当前用户的通知公告列表")
    @PostMapping("/listByCurrentUser")
    public TableDataInfo listByCurrentUser(@RequestBody SysNotice notice) {
        startPage(notice);
        List<SysNoticeVO> list = noticeUserService.selectNoticeListByCurrentUser(notice, getUserId());
        return getDataTable(list);
    }

    @Operation(summary = "收藏 / 取消收藏 公告消息")
    @PutMapping("/updatePersonNoticeHasLike/{id}/{hasLike}")
    public AjaxResult updatePersonNoticeHasLike(@PathVariable String id, @PathVariable Integer hasLike) {
        noticeUserService.updatePersonNoticeHasLike(id, hasLike);
        return success();
    }

    @Operation(summary = "置为已读")
    @PutMapping("/updatePersonNoticeHasRead/{id}")
    public AjaxResult updatePersonNoticeHasRead(@PathVariable String id) {
        noticeUserService.updatePersonNoticeHasRead(id);
        return success();
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @Operation(summary = "根据通知公告编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable String noticeId) {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @Operation(summary = "新增通知公告")
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        noticeService.insertNotice(notice);
        return success();
    }

    /**
     * 修改通知公告
     */
    @Operation(summary = "修改通知公告")
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        noticeService.updateNotice(notice);
        return success();
    }

    /**
     * 删除通知公告
     */
    @Operation(summary = "删除通知公告")
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable String[] noticeIds) {
        noticeService.deleteNoticeByIds(noticeIds);
        return success();
    }
}
